<template>
  <el-dialog v-model="dialogVisible" title="编辑" width="400px" append-to-body>
    <el-form ref="dialogFormRef" :model="dialogForm" :rules="rules" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dialogForm.name" />
      </el-form-item>
      <el-form-item label="版本" prop="version">
        <el-select v-model="dialogForm.version">
          <el-option v-for="(item, index) in versions" :key="'version-item' + index" :value="item" :label="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="版本数值" prop="versionNum">
        <el-input v-model="dialogForm.versionNum" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="handleSure">确 定</el-button>
        <el-button :loading="buttonLoading" @click="handleCancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="DetailEditDialog" lang="ts">
import { editStrengthDetail } from '@/api/bomCompareTool/strengthModelList/index';
// 获取当前实例
const { proxy } = getCurrentInstance();
const { datas, dataId } = defineProps({
  datas: {
    type: Object,
    default: () => ({})
  },
  dataId: {
    type: String
  }
});

const emit = defineEmits(['on-close', 'on-sure']);
const rules = reactive<ElFormRules>({
  name: [{ required: true, message: '名称不能为空', trigger: 'change' }],
  version: [{ required: true, message: '版本不能为空', trigger: 'change' }],
  versionNum: [{ required: true, message: '版本数值不能为空', trigger: 'change' }]
});
const dialogFormRef = ref<ElFormInstance>();
const versions = ref<Array<any>>(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J']);
const dialogVisible = ref(true);
const buttonLoading = ref(false);
const dialogForm = ref({
  name: datas.name,
  version: datas.version,
  versionNum: datas.versionNum
});
const handleSure = async () => {
  await dialogFormRef.value.validate((valid, fields) => {
    if (valid) {
      const obj = { ...datas };
      obj.name = dialogForm.value.name;
      obj.version = dialogForm.value.version;
      obj.versionNum = dialogForm.value.versionNum;
      for (const key in obj) {
        if (versions.value.includes(key)) {
          delete obj[key];
        }
      }
      editStrengthDetail(obj.aircraftModel, dataId, [obj]).then((res) => {
        if (res.code === 200) {
          emit('on-sure');
          emit('on-close');
          proxy?.$modal.msgSuccess('编辑成功！');
        }
      });
    } else {
      proxy?.$modal.msgError('请填写完整数据！');
    }
  });
};
const handleCancel = () => {
  emit('on-close');
};
</script>

<style lang="scss" scoped></style>
